package com.lx.test.web;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//



import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo.Builder;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Component
@Order(5)
public class HsafRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    private Logger log = LoggerFactory.getLogger(HsafRequestMappingHandlerMapping.class);

    public HsafRequestMappingHandlerMapping() {
        setOrder(3);
    }

    protected boolean isHandler(Class<?> beanType) {
        return AnnotatedElementUtils.hasAnnotation(beanType, HsafRestPath.class);
    }

    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
        HsafRestPath hp_clz = (HsafRestPath)AnnotatedElementUtils.findMergedAnnotation(handlerType, HsafRestPath.class);
        HsafRestPath hp_method = (HsafRestPath)AnnotatedElementUtils.findMergedAnnotation(method, HsafRestPath.class);
        if (hp_clz != null && hp_method != null) {
            String clz_path = hp_clz.value() == null ? "" : hp_clz.value().trim();
            String method_path = hp_method.value() == null ? "" : hp_method.value().trim();
            RequestMethod[] rm = hp_method.method();
            String url;
            if (clz_path.equals("/") && method_path.startsWith("/")) {
                url = method_path;
            } else {
                url = clz_path + method_path;
            }

            this.log.info("registerHandler,url=" + url + ";method=" + method);
            Builder builder = RequestMappingInfo.paths(new String[]{url});
            if (rm != null && rm.length > 0) {
                builder.methods(rm);
            } else {
                builder.methods(new RequestMethod[]{RequestMethod.GET, RequestMethod.POST});
            }

            return builder.build();
        } else {
            return null;
        }
    }
}
